//
//  TTWebService.h
//  Tint
//
//  Created by Kevin Wu on 2/29/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TTCore.h"
#import "TTAdditions.h"
#import "TTBasic.h"

#import "TTCaches.h"
#import "TTHTTPRequest.h"
#import "TTPageManager.h"


@interface TTWebService : NSObject<TTOperationDelegate, TTDelegatesProtocol> {
    NSString *_serviceAddress;
    NSData *_responseData;
    TTHTTPRequest *_request;
    NSTimeInterval _requestTimeoutInterval;
    NSString *_requestMethod;
    NSData *_requestBody;
    
    id _parsedObject;
    
    NSTimeInterval _cacheTimeoutInterval;
    NSString *_cacheKey;
    
    
    BOOL _loading;
    BOOL _cancelled;
    
    
    NSMutableDictionary *_pageManagerMap;
    
    NSMutableDictionary *_parameters;
    
    TTDiskCache *_diskCache;
    
    NSMutableArray *_delegates;
}

@property(nonatomic, copy) NSString *serviceAddress;
@property(nonatomic, retain) NSData *responseData;
@property(nonatomic, readonly) TTHTTPRequest *request;
@property(nonatomic, assign) NSTimeInterval requestTimeoutInterval;
@property(nonatomic, copy) NSString *requestMethod;
@property(nonatomic, retain) NSData *requestBody;

@property(nonatomic, readonly) id parsedObject;

@property(nonatomic, assign) NSTimeInterval cacheTimeoutInterval;
@property(nonatomic, copy) NSString *cacheKey;

@property(nonatomic, readonly) BOOL loading;
@property(nonatomic, readonly) BOOL cancelled;


+ (id)serviceByTypeNumber:(NSUInteger)number;
+ (id)serviceByType:(NSString *)type;
+ (void)destroyServiceByNumber:(NSUInteger)number;
+ (void)destroyServiceByType:(NSString *)type;
+ (id)sharedObject;

- (void)setUp;

- (void)setParameterDictionary:(NSDictionary *)dict;
- (void)addParameter:(NSString *)parameter forKey:(NSString *)key;
- (void)addAllPropertiesAsParameter;
- (void)addAllPropertiesAsParameterExcept:(NSString *)propertyString;
- (void)addPropertiesAsParameter:(NSString *)propertyString;
- (void)removeParameterForKey:(NSString *)key;
- (NSString *)parameterForKey:(NSString *)key;
- (void)clearAllParameters;

- (void)start; // Request immediately, no cache
- (void)startWithCacheTimeoutInterval:(NSTimeInterval)timeoutInterval; // Request depends on cache, cache
- (void)refreshWithCacheTimeoutInterval:(NSTimeInterval)timeoutInterval; // Request immediately, cache
- (void)cancel;

- (void)parse:(NSData *)data;

- (void)clearRequest;
- (void)clearResult;
- (void)clearAll;
- (void)setNewParsedObject:(id)object;

- (BOOL)isInCache;
- (NSDate *)updateDate;

- (TTPageManager *)defaultPageManager;
- (TTPageManager *)pageManagerForKey:(NSString *)key;
- (void)setPageManager:(TTPageManager *)pageManager forKey:(NSString *)key;
- (void)clearAllPageManager;

- (void)webServiceAlreadyInCache;
- (void)webServiceDidStart;
- (void)webServiceDidFail:(NSError *)error;
- (void)webServiceWillFinish;
- (void)webServiceDidFinish;


// To override
- (id)objectToBeParsed:(NSData *)data;
- (id)parseObject:(id)object;
- (void)saveResult:(id)result;

- (NSString *)stringValueForProperty:(NSString *)name;

@end



@protocol TTWebServiceDelegate <NSObject>
@optional
- (void)webServiceAlreadyInCache:(TTWebService *)service;
- (void)webServiceDidStart:(TTWebService *)service;
- (void)webServiceDidFail:(TTWebService *)service withError:(NSError *)error;
- (void)webServiceWillFinish:(TTWebService *)service;
- (void)webServiceDidFinish:(TTWebService *)service;
@end
